<?php

class M_kalender extends CI_Model{
	
	function M_kalender(){
		parent::__construct();
	}
	
	function get_list(){
		$query = "SELECT TANGGAL, JENISLIBUR, AGAMA, KETERANGAN, USERNAME
		FROM kalenderlibur";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($tanggal,$jenislibur,$agama,$keterangan,$username){
		$sql = "SELECT * FROM kalenderlibur WHERE USERNAME='".$username."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"TANGGAL"=>$tanggal,
					"JENISLIBUR"=>$jenislibur,
					"AGAMA"=>$agama,
					"KETERANGAN"=>$keterangan,
					"USERNAME"=>$username
			);
			$this->db->insert('kalenderlibur', $data);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 0;
			}
		}
	}
	
	function update($tanggal,$jenislibur,$agama,$keterangan,$username){
		$data = array(
					"TANGGAL"=>$tanggal,
					"JENISLIBUR"=>$jenislibur,
					"AGAMA"=>$agama,
					"KETERANGAN"=>$keterangan,
					"USERNAME"=>$username
			);
		$this->db->where('USERNAME', $username);
		$this->db->update('kalenderlibur', $data);
		
		return 1;
	}
	
	function delete($tanggal){
		$this->db->where('TANGGAL', $tanggal);
		$this->db->delete('kalenderlibur');
		
		return 1;
	}
	
}

?>